package com.xhu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhu.common.ResponseInfo;
import com.xhu.common.request.Page;
import com.xhu.common.request.SaleplanCommitRequest;
import com.xhu.common.request.GetByMonthReq;
import com.xhu.pojo.Saleplan;
import com.xhu.service.ISaleplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.xhu.constant.ResultConstant.SUCCESS;

@RestController
@RequestMapping("/saleplan")
public class SaleplanController {

    @Autowired
    private ISaleplanService service;


    @PostMapping("/queryAllPlans")
    public String queryAllPlans(@RequestBody Page page) {
        IPage<Saleplan> result = service.selectSaleplanToPages(page.getCurrent(), page.getSize());
        ResponseInfo<Map<String, Object>> response = new ResponseInfo<>();

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("pageCount", result.getPages());
        resMap.put("content", result.getRecords());

        response.setStatus(SUCCESS);
        response.setData(resMap);

        return JSON.toJSONString(response);
    }

    @PostMapping("/addPlan")
    public String addPlan(@RequestBody Saleplan saleplan) {
        service.addPlan(saleplan);

        ResponseInfo<Void> res = new ResponseInfo<Void>()
                .setMsg("添加成功")
                .setStatus(SUCCESS);

        return JSON.toJSONString(res);
    }

    @PostMapping("/commit")
    public String commitById(@RequestBody SaleplanCommitRequest entity) {

        Saleplan saleplan = new Saleplan().setSaleplanId(entity.getId())
                .setCurrentName(entity.getCurrentName());

        service.commitPlan(saleplan);
        ResponseInfo<Void> res = new ResponseInfo<Void>()
                .setMsg("提交成功")
                .setStatus(SUCCESS);

        return JSON.toJSONString(res);
    }

    @PostMapping("/update")
    public String modifyById(@RequestBody Saleplan saleplan) {
        service.updatePlan(saleplan);

        ResponseInfo<Void> res = new ResponseInfo<Void>()
                .setMsg("修改成功")
                .setStatus(SUCCESS);

        return JSON.toJSONString(res);

    }

    @PostMapping("/delete")
    public String deletePlan(@RequestBody String formId) {

        //取出json中的id
        String id = JSON.parseObject(formId, Map.class).get("id").toString();

        service.deleteById(id);

        ResponseInfo<Void> res = new ResponseInfo<Void>()
                .setMsg("删除成功")
                .setStatus(SUCCESS);

        return JSON.toJSONString(res);
    }

    @PostMapping("/getByDate")
    public String getPlansByDatetime(@RequestBody GetByMonthReq entity) {

        IPage<Saleplan> result = service.getByDateTime(entity.getCurrentPage(), entity.getSize(),
                entity.getStringTime());

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("pageCount", result.getPages());
        resMap.put("content", result.getRecords());

        ResponseInfo<Map<String, Object>> res = new ResponseInfo<Map<String, Object>>()
                .setMsg("查找成功")
                .setData(resMap)
                .setStatus(SUCCESS);

        return JSON.toJSONString(res);

    }
}
